import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Hugh
 * @Date: 2020/12/18
 */
public class Test1 {

    public static void main(String[] args) throws InterruptedException {
        final Buf buf = new Buf();
        ExecutorService es = Executors.newFixedThreadPool(11);
        for (int i = 0; i < 1; i++) {
            es.execute(new Runnable() {
                @Override
                public void run() {
                    while (true ) {
                        try {
                            int i1 = new Random().nextInt(10);
                            buf.put(i1);
                            System.out.println(Thread.currentThread().getName() + "  put" + i1);
                            Thread.sleep(2000);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            });
        }
        for (int i = 0; i < 10; i++) {
            es.execute(new Runnable() {
                @Override
                public void run() {
                    while (true ) {
                        try {
                            int i1 = buf.get();
                            System.out.println(Thread.currentThread().getName() + "  put" + i1);
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            });
        }
        es.shutdown();
        es.awaitTermination(1, TimeUnit.MINUTES);
    }
}
